package opengl.platform.ohos;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class ETC1Util {

    /**
     * A utility class encapsulating a compressed ETC1 texture.
     */
    public static class ETC1Texture {
        public ETC1Texture(int width, int height, ByteBuffer data) {
            mWidth = width;
            mHeight = height;
            mData = data;
        }

        /**
         * Get the width of the texture in pixels.
         *
         * @return the width of the texture in pixels.
         */
        public int getWidth() {
            return mWidth;
        }

        /**
         * Get the height of the texture in pixels.
         *
         * @return the width of the texture in pixels.
         */
        public int getHeight() {
            return mHeight;
        }

        /**
         * Get the compressed data of the texture.
         *
         * @return the texture data.
         */
        public ByteBuffer getData() {
            return mData;
        }

        private int mWidth;
        private int mHeight;
        private ByteBuffer mData;
    }

    public ETC1Util() {
    }

    public static void loadTexture(int target, int level, int border, int fallbackFormat, int fallbackType, InputStream input) throws IOException {
        //ohos.opengl.ETC1Util.loadTexture(target, level, border, fallbackFormat, fallbackType, input);
        throw new RuntimeException("call not implemented method");
    }

    public static void loadTexture(int target, int level, int border, int fallbackFormat, int fallbackType, ETC1Texture texture) {
        //ohos.opengl.ETC1Util.loadTexture(target, level, border, fallbackFormat, fallbackType, texture);
        throw new RuntimeException("call not implemented method");
    }

    public static boolean isETC1Supported() {
        throw new RuntimeException("call not implemented method");
    }

    public static ETC1Texture createTexture(InputStream input) throws IOException {
        int width = 0;
        int height = 0;
        byte[] ioBuffer = new byte[4096];
        {
            if (input.read(ioBuffer, 0, ETC1.ETC_PKM_HEADER_SIZE) != ETC1.ETC_PKM_HEADER_SIZE) {
                throw new IOException("Unable to read PKM file header.");
            }
            ByteBuffer headerBuffer = ByteBuffer.allocateDirect(ETC1.ETC_PKM_HEADER_SIZE)
                    .order(ByteOrder.nativeOrder());
            headerBuffer.put(ioBuffer, 0, ETC1.ETC_PKM_HEADER_SIZE).position(0);
            if (!ETC1.isValid(headerBuffer)) {
                throw new IOException("Not a PKM file.");
            }
            width = ETC1.getWidth(headerBuffer);
            height = ETC1.getHeight(headerBuffer);
        }
        int encodedSize = ETC1.getEncodedDataSize(width, height);
        ByteBuffer dataBuffer = ByteBuffer.allocateDirect(encodedSize).order(ByteOrder.nativeOrder());
        for (int i = 0; i < encodedSize; ) {
            int chunkSize = Math.min(ioBuffer.length, encodedSize - i);
            if (input.read(ioBuffer, 0, chunkSize) != chunkSize) {
                throw new IOException("Unable to read PKM file data.");
            }
            dataBuffer.put(ioBuffer, 0, chunkSize);
            i += chunkSize;
        }
        dataBuffer.position(0);
        return new ETC1Texture(width, height, dataBuffer);
    }

    public static ETC1Texture compressTexture(Buffer input, int width, int height, int pixelSize, int stride) {
        throw new RuntimeException("call not implemented method");
    }

    public static void writeTexture(ETC1Texture texture, OutputStream output) throws IOException {
        throw new RuntimeException("call not implemented method");
    }

}
